var MyMar;

function modelInit() {
    // $.cookie('TV_CST_userName', '王工头', {
    //     expires: 7
    // });
    var indexList = new TV.CookieOpt().getOpt()
    // console.log(indexList)
    // var index = indexList && indexList.index ? indexList.index : 0;
    var index = indexList && typeof indexList.index == 'number' ? indexList.index : undefined;
    var page = indexList && indexList.page ? indexList.page : 1;
    if (!modelIndex.disUser) {
        TV.UserMess()
    }
    //  TV.getHeader();
    getMessage(page, modelIndex.pageSize, 'next', index);
}

//标题过长滚动
function timedCount(index, str) {
    var elem = str;
    var thisContain = elem[index];
    console.info(thisContain);
    // debugger
    var thisIntro = elem[index].getElementsByClassName("sb");
    thisIntro[0].style.left = '0px';

    function Marquee() {
        var left = thisIntro[0].style.left;
        if (thisContain.offsetWidth < thisIntro[0].offsetWidth) {
            if (parseInt(left) > -thisIntro[0].offsetWidth) {
                thisIntro[0].style.left = (parseInt(left) - 1) + 'px';
            } else {
                thisIntro[0].style.left = '0px'
            }
        }
    }
    MyMar = setInterval(Marquee, 40);
}


/*
 * 1.右侧区
 */

cmsIndex.modules[cmsIndex.moduleGuid(cmsIndex.addModule())] = {
    modName: 'right',
    modNum: cmsIndex.moduleGuid(),
    map: {
        left: 0
    }
};

var right = cmsIndex.modules[cmsIndex.moduleGuid()].module = (function () {
    var mod = {
            modName: "pageList",
            modNum: cmsIndex.moduleGuid()
        },
        index = modelIndex.index, //位置
        isLike = modelIndex.isLike, //是否开启点赞功能
        special = modelIndex.special ? modelIndex.special : '',
        specialState = modelIndex.specialState,
        status = '1', //是否在外框
        length = 0, //真实长度
        nowPage = 1, //当前页数
        totalPage = 1, //总页数
        type = modelIndex.type, //类型
        listName = modelIndex.listName, //模块名
        pageSize = modelIndex.pageSize, //模块数量
        rowHeight = modelIndex.rowHeight, //多行模块行数
        containerId = modelIndex.containerId; //包含模块的标签id

    //操作
    mod.toggleFocus = function (number) {
        var me = mod;
        var elemSpe = '';
        if (number != undefined) {
            index = number;
        }
        console.log(index)
        if (length == 0 && special) {
            index = -2;
        }
        var elem = $("#" + containerId).children("." + listName);
        var elemCur = $("#" + containerId).children(".cur")
        if (special) {
            elemSpe = $("." + special)[0];
            elemSpe.className = special;
        }

        for (i = 0; i < length; i++) {
            elem[i].className = listName;
            if (elemCur.length) {
                elemCur[i].className = 'cur';
            }

        }

        console.log(index)
        if (index < -1 && special) {
            elemSpe.className = elemSpe.className + ' spActive';
        } else if (elem.length) {
            if (status == '0') {
                elemCur[index].className = elemCur[index].className + ' curActive'
            }
            elem[index].className = elem[index].className + ' active';

            if ($(".list-title").length) {
                clearInterval(MyMar);

                timedCount(index, $(".list-title"));
            } else {
                // clearInterval(MyMar);

                // timedCount(index,$(".list-title"));
            }


        }
    };
    //赋值
    mod.initialization = function () {
        var me = mod;
        nowPage = parseInt(document.getElementById('now_page').innerHTML);
        totalPage = parseInt(document.getElementById('total_page').innerHTML);
        length = $('#' + containerId).children("." + listName).length;
        return me;
    };
    //index 位置调整
    mod.moveOp = function (directive) {
        var me = mod;
        console.log(specialState, nowPage)
        if (specialState == 'didi' && index == length - 1 && directive == 'down') {
            index = -2;
        } else if (specialState == 'didi' && index == -2 && length > 0) {
            if (directive == 'up') {
                index = length - 1;
            }

        } else {
            var json = new TV.ListOperation({
                type: type,
                key: directive,
                isLike: isLike,
                status: status,
                nowPage: nowPage,
                totalPage: totalPage,
                pageSize: pageSize,
                length: length,
                index: index,
                rowHeight: rowHeight
            })
            var message = json.operation();
            console.log(message)
            index = message.index;
            status = message.status;
            if (message.position != '') {
                if (index >= length) {
                    index = 0
                }
                getMessage(message.nowPage, pageSize, message.position); //下一页
            }
        }
        mod.toggleFocus();
        return me;
    };
    mod.action = function (directive) {
        var me = mod;
        switch (directive) {
            case 'back':
                if (nowPage != 1) {
                    document.getElementById('now_page').innerHTML = 1
                    nowPage = 2
                    index = 0
                    mod.moveOp("backwards")
                } else {
                    history.go(-1);

                }
                break;
            case 'enter':
                if (isCache) {
                    var cookieTv = new TV.CookieOpt({
                        index: index,
                        page: nowPage
                    })
                    var indexList = cookieTv.setOpt();
                }

                direEnter(index);

                break;
        }
        return me;
    };
    mod.grabEvent = function (key) {
        var me = mod;
        var keyJson = TV.GetKey(key);
        if (keyJson.keyState && keyJson.keyState != 'enter' && keyJson.keyState != 'back') {
            me.moveOp(keyJson.keyState);
        } else {
            me.action(keyJson.keyState)
        }
        return keyJson.isBlock;


    };

    return mod;
})();

cmsIndex.run();